local CSGuildWarReportInfo = class("CSGuildWarBattleInfo")
local CSGuildWarGuildInfo = import(".CSGuildWarGuildInfo")
local CSGuildWarBattleInfo = import(".CSGuildWarBattleInfo")


function CSGuildWarReportInfo:ctor()
 
end

function CSGuildWarReportInfo:read(pbData)
    if not pbData then
        return
    end
    local guild1Data = rawget(pbData, "guild1")
    local guild1 = nil
    if guild1Data then
        guild1 = CSGuildWarGuildInfo.new()
        guild1:read(guild1Data)
    end

    local guild2Data = rawget(pbData, "guild2")
    local guild2 = nil
    if guild2Data then
        guild2 = CSGuildWarGuildInfo.new()
        guild2:read(guild2Data)
    end

    local isReverse = false
    if guild1 and guild2 then
        if guild1:isMyGuild() then
            isReverse = false
        elseif guild2:isMyGuild() then
            isReverse = true
        else
            local serverIndex1 = guild1:getServerIndex()
            local serverIndex2 = guild2:getServerIndex()
            if serverIndex1 == serverIndex2 then
                isReverse = false
            else
                local curServer = tonumber(app.session:getZoneId() or 0)
                if serverIndex1 == curServer then
                    isReverse = false
                elseif serverIndex2 == curServer then
                    isReverse = true
                else
                    if serverIndex1 < serverIndex2 then
                        isReverse = false
                    else
                        isReverse = true
                    end
                end
            end
        end
        if isReverse then
            self._guild1 = guild2
            self._guild2 = guild1
        else
            self._guild1 = guild1
            self._guild2 = guild2
        end
    else
        self._guild1 = guild1
        self._guild2 = guild2
    end
    local reports = {}
    for k, v in pairs(rawget(pbData, "battles") or {}) do
        local info = CSGuildWarBattleInfo.new()
        info:read(v, isReverse)
        table.insert(reports, info)
    end

    table.sort(reports, function(a, b)
        return a:getRound() < b:getRound()
    end)

    self._reports = reports

end

function CSGuildWarReportInfo:getReports()
    return self._reports or {}
end

function CSGuildWarReportInfo:getGuild1()
    return self._guild1
end

function CSGuildWarReportInfo:getGuild2()
    return self._guild2
end


return CSGuildWarReportInfo